//
//  Cell_Comment.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/25.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "Cell_Comment.h"
#import "JudgeStarsView.h"

@interface Cell_Comment () {}
@property (weak, nonatomic) IBOutlet UIImageView *headView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet JudgeStarsView *starView;
@property (weak, nonatomic) IBOutlet UILabel *starsLabel;
@property (weak, nonatomic) IBOutlet UILabel *contextLabel;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIButton *messageButton;
@property (weak, nonatomic) IBOutlet UIButton *praiseButton;
@property (weak, nonatomic) IBOutlet UILabel *commentsLabel;
@property (weak, nonatomic) IBOutlet UIView *commentsBgView;


@property (nonatomic, copy) CellCommentCallBackBlock block;
@property (nonatomic, strong) Model_AllComment *model;
@end
@implementation Cell_Comment

- (void)awakeFromNib {
    [super awakeFromNib];
    self.headView.layer.masksToBounds = YES;
    self.headView.layer.cornerRadius = self.headView.mj_h * 0.5;

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Cell_Comment class]) owner:self options:nil] lastObject];
    }
    return self;
}

- (void)refreshWithModel:(Model_AllComment *)model CallBackBlock:(CellCommentCallBackBlock)block {
    self.model = model;
    self.block = block;
    
    [self.headView sd_setImageWithURL:[NSURL URLWithString:model.headUrl] placeholderImage:[UIImage imageNamed:@"un_audio_icon"]];
    self.userNameLabel.text = model.userName;
    self.starView.stars = model.stars;
    self.starsLabel.text = [NSString stringWithFormat:@"%.2f", model.stars];
    self.contextLabel.text = model.comments;
    self.contextLabel.font = model.commentsFont;
    self.commentsLabel.attributedText = model.replies;
    if (model.replies.length > 0) {
        self.commentsBgView.hidden = NO;
        self.commentsLabel.hidden = NO;
    }else {
        self.commentsBgView.hidden = YES;
        self.commentsLabel.hidden = YES;
    }
    self.dateLabel.text = [NSString stringWithFormat:@"%ld天前", model.timeInterval];
    [self.messageButton setTitle:[NSString stringWithFormat:@"%d", model.repliesCount] forState:UIControlStateNormal];
    [self.praiseButton setTitle:[NSString stringWithFormat:@"%d", model.praiseCount] forState:UIControlStateNormal];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark --mark EVENT


/**
 评论

 @param sender sender
 */
- (IBAction)commentButtonClick:(UIButton *)sender {
    if (self.block) {
        self.block(CellCommentButtonClickTypeMeesage);
    }
}

/**
 点赞

 @param sender sender
 */
- (IBAction)praiseButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        int s = [sender.titleLabel.text intValue];
        s += 1;
        [sender setTitle:[NSString stringWithFormat:@"%d", s] forState:UIControlStateSelected];
        [sender setTitle:[NSString stringWithFormat:@"%d", s] forState:UIControlStateNormal];
    }else {
        int s = [sender.titleLabel.text intValue];
        s -= 1;
        [sender setTitle:[NSString stringWithFormat:@"%d", s] forState:UIControlStateNormal];
        [sender setTitle:[NSString stringWithFormat:@"%d", s] forState:UIControlStateSelected];
    }
    if (self.block) {
        self.block(CellCommentButtonClickTypePraise);
    }
}


@end
